package com.xzl.manger.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzl.manger.model.Goods;
import com.xzl.manger.model.Order;
import com.xzl.manger.model.OrderGood;
import com.xzl.manger.server.OrderServer;
import com.xzl.shop.model.User;
import com.xzl.shop.server.UserServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("order")
public class OrderController {
    @Autowired
    private OrderServer orderServer;
    @Autowired
    private UserServer userServer;
    @RequestMapping("ok")
    public ModelAndView orderOk(Order order){
        ModelAndView mv = new ModelAndView();
        PageHelper.startPage(order.getPageNum(),order.getPageSize());
        List<Order> list = orderServer.selectOrderOk(new Order());
        PageInfo pageInfo = new PageInfo(list);
        mv.addObject("pageinfo",pageInfo);
        List<User> users = userServer.selectUser(new User());
        mv.addObject("users",users);
        mv.setViewName("Orders/orderOk");
        return mv;
    }
    @RequestMapping("no")
    public ModelAndView orderNo(Order order){
        ModelAndView mv = new ModelAndView();
        PageHelper.startPage(order.getPageNum(),order.getPageSize());
        List<Order> list = orderServer.selectOrderNo(new Order());
        PageInfo pageInfo = new PageInfo(list);
        mv.addObject("pageinfo",pageInfo);
        List<User> users = userServer.selectUser(new User());
        mv.addObject("users",users);
        mv.setViewName("Orders/orderNo");
        return mv;
    }
    @RequestMapping("all")
    public ModelAndView orderAll(Order order){
        ModelAndView mv = new ModelAndView();
        List<Order> list = orderServer.selectOrder(order);
        mv.addObject("list",list);
        mv.setViewName("Orders/orderAll");
        return mv;
    }
    @RequestMapping("refund")
    public ModelAndView refund(String ids){
        orderServer.refundOrders(ids);
        ModelAndView mv = new ModelAndView();
        mv = orderOk(new Order());
        return mv;
    }
    @RequestMapping("express")
    public ModelAndView express(String ids){
        orderServer.expressOrders(ids);
        ModelAndView mv = new ModelAndView();
        mv = orderOk(new Order());
        return mv;
    }
    @RequestMapping("refundMoney")
    public ModelAndView refundMoney(String ids){
        orderServer.refundMoney(ids);
        ModelAndView mv = new ModelAndView();
        mv = orderNo(new Order());
        return mv;
    }
    @RequestMapping("detail")
    public ModelAndView detail(String orderId){
        OrderGood orderGood =new OrderGood();
        orderGood.setOrderId(orderId);
        List<OrderGood> list = orderServer.selectOrderGood(orderGood);
        ModelAndView mv = new ModelAndView();
        mv.addObject("list",list);
        mv.setViewName("Orders/orderGood");
        return mv;
    }
    @RequestMapping("pay")
    public String pay(String ids, HttpServletRequest req){
        HttpSession session = req.getSession();
        Integer userId = (Integer) session.getAttribute("userid");
        BigDecimal b = orderServer.pay(ids,userId);
        if(b.equals(BigDecimal.ZERO)) return "支付失败";
        else return "成功支付"+b+"元";
    }
    @RequestMapping("delete")
    public void delete(String ids){
        Integer i = orderServer.delete(ids);
    }
    @RequestMapping("receive")
    public void receive(String ids){
        orderServer.receiveOrders(ids);
    }
}
